{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 数据重采样\n",
    " * 时间数据由一个频率转换到另一个频率\n",
    " * 降采样\n",
    " * 升采样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.399750\n",
       "2011-01-02    0.209137\n",
       "2011-01-03   -0.042326\n",
       "2011-01-04   -1.025412\n",
       "2011-01-05    0.790592\n",
       "Freq: D, dtype: float64"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = pd.date_range('1/1/2011', periods=90, freq='D')\n",
    "ts = pd.Series(np.random.randn(len(rng)), index=rng)\n",
    "ts.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-31    2.382402\n",
       "2011-02-28   -3.923788\n",
       "2011-03-31    1.753588\n",
       "Freq: M, dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为月级别\n",
    "ts.resample('M').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.232939\n",
       "2011-01-04    0.436337\n",
       "2011-01-07    0.112013\n",
       "2011-01-10   -1.989391\n",
       "2011-01-13    2.479883\n",
       "2011-01-16   -4.078358\n",
       "2011-01-19    0.776364\n",
       "2011-01-22   -0.165976\n",
       "2011-01-25    4.341528\n",
       "2011-01-28    0.218991\n",
       "2011-01-31    2.751273\n",
       "2011-02-03   -2.042759\n",
       "2011-02-06   -4.307406\n",
       "2011-02-09   -0.522396\n",
       "2011-02-12   -1.235091\n",
       "2011-02-15   -0.902678\n",
       "2011-02-18    0.406226\n",
       "2011-02-21    0.445058\n",
       "2011-02-24    1.021034\n",
       "2011-02-27    0.390108\n",
       "2011-03-02   -3.337270\n",
       "2011-03-05   -0.029043\n",
       "2011-03-08   -1.266136\n",
       "2011-03-11   -3.068628\n",
       "2011-03-14    2.292505\n",
       "2011-03-17   -0.699373\n",
       "2011-03-20    3.432670\n",
       "2011-03-23    0.924593\n",
       "2011-03-26    1.586529\n",
       "2011-03-29    2.474536\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 转换为3天级别\n",
    "ts.resample('3D').sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.077646\n",
       "2011-01-04    0.145446\n",
       "2011-01-07    0.037338\n",
       "2011-01-10   -0.663130\n",
       "2011-01-13    0.826628\n",
       "2011-01-16   -1.359453\n",
       "2011-01-19    0.258788\n",
       "2011-01-22   -0.055325\n",
       "2011-01-25    1.447176\n",
       "2011-01-28    0.072997\n",
       "2011-01-31    0.917091\n",
       "2011-02-03   -0.680920\n",
       "2011-02-06   -1.435802\n",
       "2011-02-09   -0.174132\n",
       "2011-02-12   -0.411697\n",
       "2011-02-15   -0.300893\n",
       "2011-02-18    0.135409\n",
       "2011-02-21    0.148353\n",
       "2011-02-24    0.340345\n",
       "2011-02-27    0.130036\n",
       "2011-03-02   -1.112423\n",
       "2011-03-05   -0.009681\n",
       "2011-03-08   -0.422045\n",
       "2011-03-11   -1.022876\n",
       "2011-03-14    0.764168\n",
       "2011-03-17   -0.233124\n",
       "2011-03-20    1.144223\n",
       "2011-03-23    0.308198\n",
       "2011-03-26    0.528843\n",
       "2011-03-29    0.824845\n",
       "Freq: 3D, dtype: float64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 均值\n",
    "day3Ts = ts.resample('3D').mean()\n",
    "day3Ts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2011-01-01   -0.077646\n",
      "2011-01-02         NaN\n",
      "2011-01-03         NaN\n",
      "2011-01-04    0.145446\n",
      "2011-01-05         NaN\n",
      "                ...   \n",
      "2011-03-25         NaN\n",
      "2011-03-26    0.528843\n",
      "2011-03-27         NaN\n",
      "2011-03-28         NaN\n",
      "2011-03-29    0.824845\n",
      "Freq: D, Length: 88, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 默认插值为空\n",
    "print(day3Ts.resample('D').asfreq())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 插值方法：\n",
    " * ffill 空值取前面的值\n",
    " * bfill 空值取后面的值\n",
    " * interpolate 线性取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.077646\n",
       "2011-01-02   -0.077646\n",
       "2011-01-03         NaN\n",
       "2011-01-04    0.145446\n",
       "2011-01-05    0.145446\n",
       "                ...   \n",
       "2011-03-25         NaN\n",
       "2011-03-26    0.528843\n",
       "2011-03-27    0.528843\n",
       "2011-03-28         NaN\n",
       "2011-03-29    0.824845\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ffill(1) 对一个缺失值填充\n",
    "day3Ts.resample('D').ffill(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.077646\n",
       "2011-01-02         NaN\n",
       "2011-01-03    0.145446\n",
       "2011-01-04    0.145446\n",
       "2011-01-05         NaN\n",
       "                ...   \n",
       "2011-03-25    0.528843\n",
       "2011-03-26    0.528843\n",
       "2011-03-27         NaN\n",
       "2011-03-28    0.824845\n",
       "2011-03-29    0.824845\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "day3Ts.resample('D').bfill(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2011-01-01   -0.077646\n",
       "2011-01-02   -0.003282\n",
       "2011-01-03    0.071082\n",
       "2011-01-04    0.145446\n",
       "2011-01-05    0.109410\n",
       "                ...   \n",
       "2011-03-25    0.455294\n",
       "2011-03-26    0.528843\n",
       "2011-03-27    0.627510\n",
       "2011-03-28    0.726178\n",
       "2011-03-29    0.824845\n",
       "Freq: D, Length: 88, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# linear 线性拟合填充\n",
    "day3Ts.resample('D').interpolate('linear')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
